﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace AgLux
{
    /// <summary>
    /// //
    /// </summary>

    public class Section : Canvas
    {
        public event EventHandler FullScreen;
        public event EventHandler StandardScreen;

        private string _header;
        private List<Component> _components;
        private List<Section> _sections;

        /// <summary>
        /// //
        /// </summary>

        /// <param name="header">
        /// //
        /// </param>

        /// <param name="components">
        /// //
        /// </param>

        public Section(string header, List<Component> components, List<Section> sections)
            : base()
        {
            _header = header;
            _components = components;
            _sections = sections;

            foreach (Component component in components)
            {

                if (component.GetType().ToString().Equals("AgLux.Picture")||component.GetType().ToString().Equals("AgLux.Video"))
                {
              //  _header = component.GetType().ToString();
                component.StandardSize += new EventHandler(StandardSize);
                component.FullSize += new EventHandler(FullSize);
                }
                Children.Add(component);
            }
        }

        /// <summary>
        /// //
        /// </summary>

        /// <returns>
        /// //
        /// </returns>

        public string Header
        {
            get
            {
                return _header;
            }
        }

        /// <summary>
        /// //
        /// </summary>

        /// <returns>
        /// //
        /// </returns>

        public List<Section> Sections
        {
            get
            {
                return _sections;
            }
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="canvasWidth">
        /// //
        /// </param>

        public virtual void FitCanvas(double canvasWidth)
        {
            Width = canvasWidth;
            Height = 20;

            foreach (Component component in _components)
            {
                //component.FitCanvas(1000);
                component.FitCanvas(canvasWidth);
                component.SetValue(Canvas.TopProperty, Height);

                Height += component.Height + 20;
            }
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="sequence">
        /// //
        /// </param>

        /// <param name="includingComponents">
        /// //
        /// </param>

        /// <returns>
        /// //
        /// </returns>

        public string PrintOut(List<int> sequence, bool includingComponents)
        {
            string printOut = "";

            foreach (int number in sequence)
            {
                printOut += number + 1 + ".";
            }

            printOut += " " + _header + "\n";

            if (includingComponents)
            {
                foreach (Component component in _components)
                {
                    printOut += "   " + component.ToString() + "\n";
                }
            }

            printOut += "\n";

            sequence.Add(0);

            foreach (Section section in _sections)
            {
                printOut += section.PrintOut(sequence, includingComponents);
                sequence[sequence.Count - 1]++;
            }

            sequence.RemoveAt(sequence.Count - 1);

            return printOut;
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="sender">
        /// //
        /// </param>

        /// <param name="args">
        /// //
        /// </param>

        private void StandardSize(object sender, EventArgs args)
        {
            foreach (UIElement child in Children)
            {
                child.Visibility = Visibility.Visible;
            }

            OnStandardScreen(new EventArgs());
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="sender">
        /// //
        /// </param>

        /// <param name="args">
        /// //
        /// </param>

        private void FullSize(object sender, EventArgs args)
        {
            UIElement component = (UIElement)sender;

            SetValue(Canvas.LeftProperty, -(double)component.GetValue(Canvas.LeftProperty));
            SetValue(Canvas.TopProperty, -(double)component.GetValue(Canvas.TopProperty));

            foreach (UIElement child in Children)
            {
                child.Visibility = Visibility.Collapsed;
            }

            component.Visibility = Visibility.Visible;

            OnFullScreen(new EventArgs());
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="args">
        /// //
        /// </param>

        private void OnFullScreen(EventArgs args)
        {
            if (FullScreen != null)
            {
                FullScreen(this, args);
            }
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="args">
        /// //
        /// </param>

        private void OnStandardScreen(EventArgs args)
        {
            if (StandardScreen != null)
            {
                StandardScreen(this, args);
            }
        }
    }
}
